package com.example.wx.dailyreport.controller;


import com.example.wx.dailyreport.pojo.Role;
import com.example.wx.dailyreport.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hulinbo
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/getallrole")
    public Map<String, Object> getAllRole() {
        Map<String, Object> map = new LinkedHashMap<>();
        List<Role> list = roleService.list();
        map.put("status", 200);
        map.put("data", list);
        return map;
    }

    @GetMapping("/getroleinpageby_")
    public Map<String, Object> getUserInPageBy_(Role role, Long authority_flag, @RequestParam(defaultValue = "1") int page, int limit) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("status", 200);
        map.put("data", roleService.selectRoleInPage(role, authority_flag, page, limit));
        map.put("count", roleService.selectRoleCount(role, authority_flag));
        return map;
    }

    @PostMapping("/insertrole")
    public Map<String, Object> insertRole(Role role, Integer[] authorityids) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (roleService.insertRole(role, authorityids))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @PostMapping("/updaterole")
    public Map<String, Object> updateRole(Role role, Integer[] authorityids) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (roleService.updateRole(role, authorityids))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }


    @PostMapping("/deleterole")
    public Map<String, Object> deleteRole(Integer id) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (roleService.deleteRole(id))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

}

